import 'package:flutter/material.dart';
import 'package:p_location/common/obj/base_filter_option.dart';
import 'package:p_location/common/widget/dropdown_menu/gzx/gzx_dropdown_menu.dart';

import 'filter_item_click_listener.dart';

class DropDownView {
  static const double ITEM_HEIGHT = 40.0;

  final int index;
  final List<BaseFilterOption> filterOptions;
  final FilterItemClickListener listener;

  DropDownView(this.index, this.filterOptions, this.listener);

  GZXDropdownMenuBuilder builder;

  GZXDropdownMenuBuilder getMenu() {
    builder = GZXDropdownMenuBuilder(
        dropDownHeight: ITEM_HEIGHT *
            (filterOptions.length > 10 ? 10 : filterOptions.length),
        dropDownWidget: _buildEquipTypeListWidget(filterOptions, (filterObj) {
          listener.onFilterItemClick(index, filterObj);
        }));
    return builder;
  }

  _buildEquipTypeListWidget(List<BaseFilterOption> items,
      void itemOnTap(BaseFilterOption filterObj)) {
    return ListView.separated(
      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      itemCount: items.length,
      separatorBuilder: (BuildContext context, int index) =>
          Divider(height: 1.0),
      itemBuilder: (BuildContext context, int index) {
        BaseFilterOption filterObj = items[index];
        return GestureDetector(
          onTap: () {
            for (var item in items) {
              item.isSelected = false;
            }
            filterObj.isSelected = true;
            print('------');
            items.forEach((item) => print(item.isSelected));
            print('------');
            itemOnTap(filterObj);
//            _requestLists(); todo
          },
          child: Container(
            padding: EdgeInsets.only(left: 20, top: 10, bottom: 10),
            child: Row(children: <Widget>[
              Expanded(
                child: Text(
                  filterObj.getName(),
                  style: TextStyle(
                      color: filterObj.isSelected
                          ? Theme.of(context).primaryColor
                          : Colors.black),
                ),
              ),
              filterObj.isSelected
                  ? Icon(Icons.check,
                      color: Theme.of(context).primaryColor, size: 16)
                  : SizedBox(),
            ]),
          ),
        );
      },
    );
  }
}
